﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLKDS.DAL
{
    public class ContractManager
    {
        public ContractInfo GetContractById(int Id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryContract = from d in db.Contracts
                                   where d.Id.Equals(Id)
                                   select new ContractInfo { Id = d.Id,Name=d.Name,Code=d.Code, Description = d.Description, EmployeeId=d.EmployeeId,BasicSalary=d.BasicSalary,DStart=d.DStart,DEnd=d.DEnd};

                return queryContract.First();
            }

        }

        public IList<ContractInfo> ListContract()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryContract = from d in db.Contracts
                                         join e in db.Employees
                                         on d.EmployeeId equals e.Id
                                    select new ContractInfo { Id = d.Id, Name = d.Name, Code = d.Code, Description = d.Description, EmployeeId = d.EmployeeId, BasicSalary = d.BasicSalary, DStart = d.DStart, DEnd = d.DEnd ,EmployeeName=e.Fullname};

                return queryContract.ToList();
            }
        }


        public void CreateContract(Contract contract)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Contracts.InsertOnSubmit(contract);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="contract"></param>
        public void UpdateContract(Contract contract)
        {
            using (var db = new QLKDSDataContext())
            {
                var _contract = db.Contracts.Single(d => d.Id.Equals(contract.Id));
                _contract.Code = contract.Code;
                _contract.Name = contract.Name;
                _contract.EmployeeId = contract.EmployeeId;
                _contract.DStart = contract.DStart;
                _contract.DEnd = contract.DEnd;
                _contract.Description = contract.Description;
                _contract.BasicSalary = contract.BasicSalary;
              
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteContract(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _contract = db.Contracts.Single(d => d.Id.Equals(id));

                db.Contracts.DeleteOnSubmit(_contract);
                db.SubmitChanges();
            }
        }

    }
}
